package com.xdtech.hotel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.CardTypeDao;
import com.xdtech.hotel.model.CardType;
import com.xdtech.hotel.service.CardTypeService;
import com.xdtech.hotel.vo.CardTypeItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:10:07
 * @since 1.0
 * @see
 */
@Service
public class CardTypeServiceImpl implements CardTypeService {
	@Autowired
	private CardTypeDao cardTypeDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param entity
	 */
	public void save(CardType entity) {
		cardTypeDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<CardType> entities) {
		cardTypeDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param entity
	 */
	public void delete(CardType entity) {
		cardTypeDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		cardTypeDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param id
	 * @return
	 */
	public CardType get(Long id) {
		return cardTypeDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @return
	 */
	public List<CardType> getAll() {
		return cardTypeDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> cardTypes = null;
		long rows = 0;
		if (pg!=null) {
			Page<CardType> page = cardTypeDao.findPage(pg,"from CardType order by createTime desc", values);
			cardTypes = BeanUtils.copyListProperties(
					CardTypeItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<CardType> cardTypeList = cardTypeDao.find("from CardType order by id desc", values);
			cardTypes = BeanUtils.copyListProperties(
					CardTypeItem.class, cardTypeList);
			rows = cardTypes.size();
		}
		results.put("total", rows);
		results.put("rows", cardTypes);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateCardType(CardTypeItem item) {
		CardType cardType = null;
		if (item.getId()==null) {
			cardType = new CardType();
		}else {
			cardType = cardTypeDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(cardType, item);
		cardTypeDao.save(cardType);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param cardTypeId
	 * @return
	 */
	public CardTypeItem loadCardTypeItem(Long cardTypeId) {
		CardType cardType = cardTypeDao.get(cardTypeId);
		CardTypeItem cardTypeItem = new CardTypeItem();
		BeanUtils.copyProperties(cardTypeItem, cardType);
		return cardTypeItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteCardTypeInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:10:07
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteCardTypeInfo(List<Long> cardTypeIds) {
		for (Long id : cardTypeIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:16:38
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午6:25:07
	 * @modified by
	 * @return
	 */
	@Override
	public List<CardTypeItem> loadItems() {
		List<CardType> cardTypes = getAll();
		return CardTypeItem.createItems(cardTypes);
	}

}
